var request = require('../../utils/request.js')
Page({
  data: {
    longitude: '',
    latitude: '',
    height:'',
    markers: [
    ],
    controls: [
    {
      id: 1,
      iconPath: '/images/center.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
    }]
  },
  center(e) {
    if (e.type === 'end') {
      var that=this
      that.mapCtx.getCenterLocation({
        success: function(res){
          that.setData({
            longitude:res.longitude,
            latitude:res.latitude
          })
          that.getMark({'lng':res.longitude,'lat':res.latitude})
        }
      })
    }
  },
  getindex(e){
    for (var i = this.data.markers.length - 1; i >= 0; i--) {
      if (this.data.markers[i]['id']==e.markerId) {
        return i
      }
    }
  },
  go(e){
    my.navigateTo({
      url:'../home/order?sn='+this.data.detail['sn']
})
  },
  controltap(e) {
    var i=this.getindex(e)
    this.setData({
      detail:this.data.markers[i]
    })
    var that=this;
     my.confirm({
        title: '设备编号'+that.data.markers[i]['sn'],
        content: that.data.markers[i]['address'],
        confirmButtonText: '马上充电',
        cancelButtonText: '暂不需要',
        success: (result) => {
          if(result.confirm === true){
            my.navigateTo({
              url:'order?sn='+that.data.markers[i]['sn']
            })
          }
        },
      });
//       var allData=that.data.markers[0];
//  var temporaryData=allData.sn
//  that.setData({
//    parameter:temporaryData
//  })
    // var object=[{
    //   iconPath:allData.iconPath,
    //   latitude:allData.latitude,
    //   longitude:allData.longitude,
    //   width: 50,
    //   height: 50,
    //   callout:{
    //     content:"设备编号:"+allData.sn+"<br>"+"充电地址:"+allData.address+"\n"+"<br>"+"可充电"+allData.num+"个"
    //   }
    // }]
     that.setData({
      markers:data
    })
    // my.showActionSheet({
    //   title: '设备信息',
    //   items: [this.data.markers[i]['sn']],
    //   cancelButtonText: '取消',
    //   success: (res) => {
    //     if (res.index==0) {
    //       my.navigateTo({
    //         url:'../home/order?sn='+that.data.markers[i]['sn']
    //       })
    //     }
    //    },
    // });
  },
  enter(){
    my.navigateTo({
      url:'../home/order?sn='+this.data.detail['sn']
    })
  },
  getMark:function(data){
    var that=this;
    var success=function(data){
      that.setData({
        markers:data
      })
    }
    request.wxrequest('/Map/getFacility',data,success,that)
  },
  getLocation:function(){
    var that=this
    my.getLocation({
      success: function(res) {
        that.getMark({'lng':res['longitude'],'lat':res['latitude']})
        that.setData({
            longitude:res['longitude'],
            latitude:res['latitude']
        })
      },
      fail: function(res){
        // if (res.errMsg =='getLocation:fail auth deny') {
        //   wx.showModal({
        //     title: '请授权',
        //     content: '未授权部分功能将无法使用',
        //     showCancel: false,
        //     success: function(res) {
        //       wx.openSetting({
        //         success:function(res){
        //           that.getLocation()
        //         }
        //       })
        //     }
        //   }) 
        // }
      }
    })
  },
  clear:function(){
    this.setData({
      detail:''
    })
  },
  onLoad:function(){
    this.getLocation()
    var that = this
    my.getSystemInfo({
      success: function(res) {
        that.data.controls[0]['position']['left'] = res.windowWidth/2-20
        that.data.controls[0]['position']['top'] = res.windowHeight/2-20
        that.setData({
          height:res.windowHeight,
          controls:that.data.controls
        })
      }
    })
  },
  onReady() {
    this.mapCtx = my.createMapContext('map')
  },



  
})